//
//  ChatViewController.m
//  聊天布局
//
//  Created by J on 16/3/3.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ChatViewController.h"
#import "Message.h"
#import "MessageCell.h"
#import "UIView+Extension.h"

@interface ChatViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITextView *messageField
;

@property (weak, nonatomic) IBOutlet UIView *toolBar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *toolBarHeight;
@property(nonatomic,strong)NSArray* messages;
@end

@implementation ChatViewController

- (NSArray *)messages{
    if (!_messages) {
        //加在plist中字典数组
        NSString* path =[[NSBundle mainBundle]pathForResource:@"messages.plist" ofType:nil];
        NSArray* dictArray = [NSArray arrayWithContentsOfFile:path];
        
        //字典数组 ->模型数组
        NSMutableArray* dataArray = [NSMutableArray array];
        Message* lastMessage = nil;//记录上一条消息模型
        for (NSDictionary* dic in dictArray) {
            Message* m = [Message messageWithDic:dic];
            m.hideTime = [m.time isEqualToString:lastMessage.time];//如果时间与上一个相同 直接隐藏
            [dataArray addObject:m];
            lastMessage = m;//当前消息模型就变成了下次循环的上个模型
        }
        _messages = dataArray;
    }
    
    return _messages;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.messageField.contentInset = UIEdgeInsetsMake(5, 0, 0, 0);//设置内容内编剧
    
    
    //监听键盘弹出 更改输入工具条底部约束
    [[NSNotificationCenter defaultCenter]addObserver:self  selector:@selector(up:) name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self  selector:@selector(change:) name:UITextViewTextDidChangeNotification object:nil];

}

- (void)change:(NSNotification*)noti{
    UITextView* textView = noti.object;
    CGFloat maxHeight = 100;
    CGFloat minHeight = 34;
    CGFloat textViewH = 0;

   CGFloat height = textView.contentSize.height;
    textViewH = height;
    if (height>maxHeight) {
        textViewH = 100;
    }else if(height<minHeight){
        textViewH = 44;
    }
    
    self.toolBarHeight.constant =  textViewH;
    [self.toolBar layoutIfNeeded];
}


- (void)up:(NSNotification*)noti{
    //获得键盘高度
    CGRect frame =  [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = [UIScreen mainScreen].bounds.size.height - frame.origin.y;
//       self.bottomConstraint.constant = [UIScreen mainScreen].bounds.size.height - frame.origin.y;//改约束屏幕高度-键盘y
    
//    self.view.y  = -([UIScreen mainScreen].bounds.size.height - frame.origin.y);//第二种方法 直接改变控制器view的Y
//        [self.view layoutIfNeeded];
    
    
    self.view.transform = CGAffineTransformMakeTranslation(0, -height);//键盘退下的高度为屏幕高度 减去屏幕高度0
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.messages.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageCell* cell = [tableView dequeueReusableCellWithIdentifier:@"message"];
    cell.message = self.messages[indexPath.row];
    return cell;
    
    
}

//UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 300;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Message* message = self.messages[indexPath.row];
    
    return message.cellHeight;

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
        [self.messageField endEditing:YES];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
